@echo off

goto post_import_sql

:aa
::1250 is most feasible on M$ to repair errors in data
::recode_utf16_cp1250 POC_*.csv
copy _d15n_final_cleanup_POC_ID_email_v2.csv cp1250_d15n_final_cleanup_POC_ID_email_v2
copy _d15n_final_cleanup_POC_ID_birthnumber_v2.csv cp1250_d15n_final_cleanup_POC_ID_birthnumber_v2
copy _d15n_final_cleanup_POC_ID_firstname_surname_v2.csv cp1250_d15n_final_cleanup_POC_ID_firstname_surname_v2
copy _d15n_final_cleanup_POC_ID_hb2.csv cp1250_d15n_final_cleanup_POC_ID_hb2
goto z

:ba
::add unique 3 characters code to each file
data_rename_abc cp1250_POC* aaa
data_rename_abc cp1250_d15n* baa

move cp1250_POC* ./byside/
move cp1250_d15n* ./byside/
goto z

:ca
data_add_unique_id_sufix a*_cp1250* d 3
data_add_unique_id_sufix b*_cp1250* d 3
move a*_cp1250* ./byside/
move b*_cp1250* ./byside/
goto z

:da
::replace original column delimiter by tab
tabd uisx_*
move uisx_* ./byside/
goto z

:ea
::create headers dictionary
csvh tabd_*
goto z

:fa
::count rows
cht tabd_*
srch "ROWS" *_cht.#
move *_cht.# ./byside/
goto z

:ga
count_delimiters_newextractbad tabd_*
goto z

:ha
move cntd_bad* ./byside/
move tabd_uis* ./byside/
::recode to utf8 before loading to pg
recode_cp1250_utf8 cntd_new*
recode_cp1250_utf8 cp1250*_repaired_fixedonly.csv
move cntd_new* ./byside/
move cp1250*_repaired_fixedonly.csv ./byside/
::goto z

:ia
::import to postgresql
pgimport utf8_cntd_new*
pgimport utf8_cp1250*_repaired_fixedonly

:post_import_sql
pgrun post_import.sql
pgrun count_imported.sql
pgexport count_imported
pgrun delete_tables.sql

:z